package com.blindBox.front.handler.hanlderIntercept;

import com.blindBox.front.commonResult.MsgBackToFront;
import com.blindBox.front.handler.verify.loginVerifyImpl.UserLoginByTokenVerify;
import com.blindBox.front.token.TokenTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//验证token的拦截器
public class TokenHanlderIntercept implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        //获取token参数
        String token = request.getParameter("token");
        System.out.println("拦截器的token =" + token);
        if(token==null||token.equals("undefined")||token.equals(""))
        {
            System.out.println("滚回的概率");
           //重定向到登录界面
            response.sendRedirect("/");
            return false;
        }
        //获取token验证类
        UserLoginByTokenVerify userLoginByTokenVerify=new UserLoginByTokenVerify();
        //令牌检验成功，则放行
        if(userLoginByTokenVerify.verify(token,new TokenTool()).getStatus().equals( MsgBackToFront.HANDLE_SUCCESS))
            return true;
        //令牌检验失败,转发到指定请求，进行处理
       request.getRequestDispatcher("/").forward(request,response);
        return false;
    }
}
